package com.kczd.jinlan.view.classify.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kczd.jinlan.R;
import com.kczd.jinlan.adapter.recycleviewadapter.RecyclerViewGradviewAdapter;
import com.kczd.jinlan.model.Goods;
import com.kczd.jinlan.myview.pulltoreflush.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * yang_g
 * 最热
 * Created by Administrator on 2017/5/23.
 */

public class TottestFragment extends Fragment {

    private View view;
    private PullLoadMoreRecyclerView mPullLoadMoreRecyclerView;
    // private int mCount = 1;
    private RecyclerViewGradviewAdapter mRecyclerViewAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.moren_fragment, null);

        mPullLoadMoreRecyclerView = (PullLoadMoreRecyclerView) view.findViewById(R.id.pullLoadMoreRecyclerView);
        //mPullLoadMoreRecyclerView.setRefreshing(true);
        mPullLoadMoreRecyclerView.setGridLayout(2);
        mRecyclerViewAdapter = new RecyclerViewGradviewAdapter(getActivity());
        mPullLoadMoreRecyclerView.setAdapter(mRecyclerViewAdapter);
        mPullLoadMoreRecyclerView.setOnPullLoadMoreListener(new PullLoadMoreListener());
        getData();

        return view;
    }

    private void getData() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mRecyclerViewAdapter.addAllData(setList());
                mPullLoadMoreRecyclerView.setPullLoadMoreCompleted();
            }
        }, 1000);
    }

    private List<Goods> setList() {
        List<Goods> dataList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            Goods goods = new Goods();
            goods.setTitle("自然成熟 有机西红柿");
            goods.setWeight_dai("5kg装");
            goods.setPrice_kg("7.6/kg");
            goods.setPrice("￥19.8");
            dataList.add(goods);
        }
        return dataList;

    }


    class PullLoadMoreListener implements PullLoadMoreRecyclerView.PullLoadMoreListener {
        @Override
        public void onRefresh() {
            setRefresh();
            getData();
        }

        @Override
        public void onLoadMore() {
            //  mCount = mCount + 1;
            getData();
        }
    }

    private void setRefresh() {
        mRecyclerViewAdapter.clearData();
        //  mCount = 1;

    }

}
